import { Injectable } from "@angular/core";
import { BaseHttpService } from "../../../../shared/services/baseHttp.service";
import { Observable } from "rxjs/Observable";
import { HttpClient } from '@angular/common/http';
import { OverallPerformanceModel } from "./models/overallPerformance.model";
import { ReplaySubject } from "rxjs";
import { GovernanceProfileModel } from "./models/governanceProfile.model";
import { HttpParams } from "@angular/common/http";
import { IRelativePerformance } from "./components/companyGovernanceSummary/models/IRelativePerformance.interface";
import { CompanyGovernanceIssueModel } from "./components/companyGovernanceIssue/models/companyGovernanceIssue.model";
import { CompanyGovernanceIssueDetailModel } from "./models/companyGovernanceIssueDetail.model";
import { GovernanceIndicatorModel } from "./models/companyGovernanceIndicator.model";
import { KeyTemplateModel } from "./models/governanceKeyTemplate.model";

/**
 * Company corporate governance service
 * 
 * Author: Sorin Spiru
 * Date: 26.02.2018
 */
@Injectable()
export class CompanyGovernanceService extends BaseHttpService {
    private profileSubject = new ReplaySubject<GovernanceProfileModel>(1);
    public governanceProfile = this.profileSubject.asObservable();

    private performanceSubject = new ReplaySubject<OverallPerformanceModel>(1);
    public overallPerformance = this.performanceSubject.asObservable();
    
    private relativeSubject = new ReplaySubject<IRelativePerformance>(1);
    public relativePerformance = this.relativeSubject.asObservable();

    constructor(public http: HttpClient) {
        super();
    }

    /**
     * Get company's governance profile data
     * @param companyId Company unique identifier
     */
    public getCompanyGovernanceProfile(companyId: string): void {
        this.http.get<GovernanceProfileModel>(this.serverUrl + 'governanceserv/api/v1/governance/' + companyId).subscribe(data=>{            
            this.profileSubject.next(data);
        });
    }

    /**
     * Get company's corporate governance overall performance data
     * @param companyId Company unique identifier
     */
    public getGovernanceOverallPerformance(companyId: string): void {
        this.http.get<OverallPerformanceModel>(this.serverUrl + 'governanceserv/api/v1/company/' + companyId + '/overallPerformance').subscribe(data => {
            this.performanceSubject.next(data);
        });
    }

    /**
     * Get company's corporate governance issues
     * @param companyId Company unique identifier
     * 
     */
    public getCompanyGovernanceIssues<T>(companyId: string, overallScores: boolean): Observable<T> {
        let params: HttpParams = new HttpParams();
        params = params.set('overallScores', overallScores.toString());
        return this.http.get<T>(this.serverUrl + 'governanceserv/api/v1/company/' + companyId + '/cgissues', { params: params });
    } 

    /**
     * Get company's corporate governance relative performance data
     * @param companyId Company unique identifier
     */
    public getGovernanceRelativePerformance(companyId: string, details: boolean): Observable<IRelativePerformance> {
        let params: HttpParams = new HttpParams();
        params = params.set('details', details.toString())
        .set('excludeOverallScore', true.toString());

        return this.http.get<IRelativePerformance>(this.serverUrl + 'governanceserv/api/v1/company/' + companyId + '/relativePerformance', { params: params });
    }

    public getGovernanceSummaryBenchmark(companyId: string): Observable<any>
    {
        return this.http.get(this.serverUrl + 'governanceserv/api/v1/company/' + companyId + '/summaryBenchmark');        
    }

    /**
     * Get detailed relative performance for corporate governance
     * @param companyId Company unique identifier
     */
    public getGovernanceDetailedRelativePerformance(companyId: string): ReplaySubject<IRelativePerformance> {
        
        this.getGovernanceRelativePerformance(companyId, true).subscribe(data=>{
            this.relativeSubject.next(data);
        });

        return this.relativeSubject;
    }
    
    /**
     * Get issue details
     * @param companyId Company unique identifier
     * @param number Issue number
     */
    public getIssueDetails(companyId: string, number: string): Observable<CompanyGovernanceIssueDetailModel> {
        return this.http.get<CompanyGovernanceIssueDetailModel>(this.serverUrl + 'governanceserv/api/v1/company/' + companyId + '/cgissues/' + number + '/indicators');
    }

    /**
     * Get indicators assessment and score belonging to the provided issue within a company
     * @param companyId Company unique identifier
     * @param number Issue number
     */
    public getIssueIndicators(companyId: string, number: string): Observable<GovernanceIndicatorModel[]> {
        return this.http.get<GovernanceIndicatorModel[]>(this.serverUrl + 'governanceserv/api/v1/company/' + companyId + '/cgissues/' + number + '/indicators/scoresandasseement')
    }

    /**
     * Get get basic governance issues.
     */
    public getKeyGCTemplate(): Observable<KeyTemplateModel[]> {
        return this.http.get<KeyTemplateModel[]>(this.serverUrl + 'governanceserv/api/v1/keycgtemplate');
    }  

    ngOnDestroy(){
        if(this.relativeSubject) this.relativeSubject.unsubscribe();
    }
}